/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.entity;

import br.jm.financeiro.base.eao.EaoException;
import br.jm.financeiro.view.LoginBean;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

/**
 *
 * @author joao
 */
@Entity
@Table(name = "usuario", catalog = "salao", schema = "public")
public class Usuario implements Serializable, HttpSessionBindingListener {
    private static final long serialVersionUID = -3341865828310255610L;
    private static Map<Usuario, HttpSession> logins = new HashMap<Usuario, HttpSession>();

    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "usuario_id_usuario_seq")
    @SequenceGenerator(name = "usuario_id_usuario_seq", sequenceName = "usuario_id_usuario_seq", initialValue = 1, allocationSize = 1)
    @Basic(optional = false)
    @Column(name = "id_usuario", nullable = false)
    private Integer idUsuario;
    @Column(name = "dsc_email", length = 255)
    private String dscEmail;
    @Column(name = "dsc_pergunta", length = 255)
    private String dscPergunta;
    @Column(name = "dsc_resposta", length = 255)
    private String dscResposta;
    @Column(name = "dsc_thema", length = 40)
    private String dscThema;
    @Basic(optional = false)
    @Column(name = "nom_usuario", nullable = false, length = 255)
    private String nomUsuario;
    @Basic(optional = false)
    @Column(name = "tip_status", nullable = false, length = 255)
    private String tipStatus;
    @Basic(optional = false)
    @Column(name = "tip_usuario", nullable = false, length = 255)
    private String tipUsuario;
    @Basic(optional = false)
    @Column(name = "usr_name", nullable = false, length = 255)
    private String usrName;
    @Basic(optional = false)
    @Column(name = "usr_password", nullable = false, length = 255)
    private String usrPassword;
    @Column(name = "finaliza")
    private Boolean finaliza;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "usuario")
    @LazyCollection(LazyCollectionOption.FALSE)
    @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private List<UsuarioModulo> usuarioModuloList;
    
    private String cUsuario;
    private String cSistema;
    
    public Usuario() {
    }

    public Usuario(Integer idUsuario) {
        this.idUsuario = idUsuario;
    }

    public Usuario(Integer idUsuario, String nomUsuario, String tipStatus, String tipUsuario, String usrName, String usrPassword) {
        this.idUsuario = idUsuario;
        this.nomUsuario = nomUsuario;
        this.tipStatus = tipStatus;
        this.tipUsuario = tipUsuario;
        this.usrName = usrName;
        this.usrPassword = usrPassword;
    }

    public Integer getIdUsuario() {
        return idUsuario;
    }

    public void setIdUsuario(Integer idUsuario) {
        this.idUsuario = idUsuario;
    }

    public String getDscEmail() {
        return dscEmail;
    }

    public void setDscEmail(String dscEmail) {
        this.dscEmail = dscEmail;
    }

    public String getDscPergunta() {
        return dscPergunta;
    }

    public void setDscPergunta(String dscPergunta) {
        this.dscPergunta = dscPergunta;
    }

    public String getDscResposta() {
        return dscResposta;
    }

    public void setDscResposta(String dscResposta) {
        this.dscResposta = dscResposta;
    }

    public String getDscThema() {
        return dscThema;
    }

    public void setDscThema(String dscThema) {
        this.dscThema = dscThema;
    }

    public String getNomUsuario() {
        return nomUsuario;
    }

    public void setNomUsuario(String nomUsuario) {
        this.nomUsuario = nomUsuario;
    }

    public String getTipStatus() {
        return tipStatus;
    }

    public void setTipStatus(String tipStatus) {
        this.tipStatus = tipStatus;
    }

    public String getTipUsuario() {
        return tipUsuario;
    }

    public void setTipUsuario(String tipUsuario) {
        this.tipUsuario = tipUsuario;
    }

    public String getUsrName() {
        return usrName;
    }

    public void setUsrName(String usrName) {
        this.usrName = usrName;
    }

    public String getUsrPassword() {
        return usrPassword;
    }

    public void setUsrPassword(String usrPassword) {
        this.usrPassword = usrPassword;
    }

    public List<UsuarioModulo> getUsuarioModuloList() {
        return usuarioModuloList;
    }

    public void setUsuarioModuloList(List<UsuarioModulo> usuarioModuloList) {
        this.usuarioModuloList = usuarioModuloList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idUsuario != null ? idUsuario.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Usuario)) {
            return false;
        }
        Usuario other = (Usuario) object;
        if ((this.idUsuario == null && other.idUsuario != null) || (this.idUsuario != null && !this.idUsuario.equals(other.idUsuario))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "br.jm.financeiro.entity.Usuario[ idUsuario=" + idUsuario + " ]";
    }

    public Boolean getFinaliza() {
        return finaliza;
    }

    public void setFinaliza(Boolean finaliza) {
        this.finaliza = finaliza;
    }

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        HttpSession session = logins.remove(this);
        if (session != null) {
            session.invalidate();
        }
        
        logins.put(this, event.getSession());
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        this.cUsuario = "Saida do Sistema Usuario: ".concat(this.nomUsuario);
        this.cSistema = "";
        try {
            LoginBean.AtualizaLog(cUsuario, cSistema);
        } catch (EaoException ex) {
            Logger.getLogger(Usuario.class.getName()).log(Level.SEVERE, null, ex);
        }
        logins.remove(this);
    }
}
